﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace BBMS.DLL.DataContract
{
    public class NhanVien : BBMS.DLL.Base.ObjectBase
    {
        public Int32 ID;
        public String UserName;
        public String FullName;
        public DateTime? Birthday;
        public String Password;
        public String Phone;
        public String Mobile;
        public String Department;
        public String Position;
        public String Email;


        public NhanVien() 
        {
        }

        public NhanVien(int id, string username, string fullname, string mobile, string department, string postion)
        {
            this.ID = id;
            this.UserName = username;
            this.FullName = fullname;          
            this.Mobile = mobile;
            this.Department = department;
            this.Position = postion;

        }

        public NhanVien(int id, string username, string fullname, DateTime? birthday
            , string password, string phone, string mobile, string department, string postion, string email)
        {
            this.ID = id;
            this.UserName = username;
            this.FullName = fullname;
            this.Birthday = birthday;
            this.Password = password;
            this.Phone = phone;
            this.Mobile = mobile;
            this.Department = department;
            this.Position = postion;
            this.Email = email;
            
        }

        public override object Key
        {
            get
            {
                return this.ID;
            }
            /*set
            {
                this.ID = (int)value;
            }*/

        }

        public override void UpdateInsertedID(MySql.Data.MySqlClient.MySqlCommand cmd)
        {
            cmd.CommandText = "SELECT LAST_INSERT_ID()";
            this.ID = Convert.ToInt32(cmd.ExecuteScalar());
        }

        public string ChangePassword(int NVID, string oldPassword, string newPassword)
        {
            string msg = "Thay đổi mật khẩu thành công";
            try
            {
                MySqlConnection con = new MySqlConnection(Base.Config.ConnectionString());
                con.Open();
                MySqlCommand cmd = con.CreateCommand();

                cmd.CommandText = "select  count(*) from " + TableName + " where Password = @OldPassword and ID = @IDNhanVien";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@IDNhanVien", MySqlDbType.Int32);
                cmd.Parameters["@IDNhanVien"].Value = NVID;
                cmd.Parameters.AddWithValue("@OldPassword", MySqlDbType.VarString);
                cmd.Parameters["@OldPassword"].Value = oldPassword;
                long count = (long)cmd.ExecuteScalar();
                if (count == 0)
                {
                    msg = "Mật khẩu hiện thời không đúng.";
                }
                else
                {
                    cmd.CommandText = "update  " + TableName + " set Password = @NewPassword where Password = @OldPassword and ID = @IDNhanVien";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@IDNhanVien", MySqlDbType.Int32);
                    cmd.Parameters["@IDNhanVien"].Value = NVID;
                    cmd.Parameters.AddWithValue("@OldPassword", MySqlDbType.VarString);
                    cmd.Parameters["@OldPassword"].Value = oldPassword;
                    cmd.Parameters.AddWithValue("@NewPassword", MySqlDbType.VarString);
                    cmd.Parameters["@NewPassword"].Value = newPassword;
                    cmd.ExecuteNonQuery();
                }
                con.Close();
                con.Dispose();
            }
            catch
            {
                msg = "Thay đổi mật khẩu thất bại!";
            }
            return msg;
        }

        public override string CheckKeyCondition(MySql.Data.MySqlClient.MySqlCommand cmd, DataRow row)
        {
            string retStr = "";
            try
            {
                string[] keyFiels = this.KeyFields;
                if (keyFiels != null && keyFiels.Length > 0)
                {
                    cmd.CommandText = string.Format("select count({0}) from {1} where ", keyFiels[0], this.TableName);
                    cmd.Parameters.Clear();
                    cmd.CommandText += string.Format(" {0}=@{0} ", "UserName");
                    cmd.Parameters.AddWithValue("@UserName", row["UserName"]);
                   /* for (int i = 0; i < keyFiels.Length; i++)
                    {
                        if (i > 0) cmd.CommandText += " and ";
                        cmd.CommandText += string.Format(" {0}=@{0} ", keyFiels[i]);
                        cmd.Parameters.AddWithValue("@" + keyFiels[i], row[keyFiels[i]]);
                    }*/
                    cmd.CommandType = CommandType.Text;
                    long count = (long)cmd.ExecuteScalar();
                    if (count > 0)
                    {
                        retStr = "Existed";
                    }
                }
            }
            catch
            {
                retStr = "Error";
            }
            return retStr;
        }


        public override string Name
        {
            get
            {
                return this.UserName;
            }
        }
        public override string  TableName
        {
	        get 
	        {
                return "Staff";
	        }
        }
        
        public override string[] KeyFields
        {
            get
            {
                return new string[] { "ID" };
            }
        }

        public override string[] IDFields
        {
            get
            {
                return new string[] { "ID" };
            }
        }

        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(TableName);
            dt.Columns.Add("ID", typeof(Int32));
            dt.Columns.Add("UserName", typeof(String));
            dt.Columns.Add("FullName", typeof(String));
            dt.Columns.Add("Birthday", typeof(DateTime));
            dt.Columns.Add("Password", typeof(String));
            dt.Columns.Add("Phone", typeof(String));
            dt.Columns.Add("Mobile", typeof(String));
            dt.Columns.Add("Department", typeof(String));
            dt.Columns.Add("Position", typeof(String));
            dt.Columns.Add("Email", typeof(String));

            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["ID"] = this.ID;
            row["UserName"] = this.UserName;
            row["FullName"] = this.FullName;
            row["Birthday"] = this.Birthday;
            row["Password"] = this.Password;
            row["Phone"] = this.Phone;
            row["Mobile"] = this.Mobile;
            row["Department"] = this.Department;
            row["Position"] = this.Position;
            row["Email"] = this.Email;


            return row;
        }

        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            NhanVien obj = null;
            try
            {
                obj = new NhanVien(Convert.ToInt32(row["ID"].ToString())
                    , row["UserName"].ToString()
                    , row["FullName"].ToString()
                    , row["Birthday"].ToString() != "" ? Convert.ToDateTime(row["Birthday"].ToString()) : (DateTime?)null
                    , row["Password"].ToString()
                    , row["Phone"].ToString()
                    , row["Mobile"].ToString()
                    , row["Department"].ToString()
                    , row["Position"].ToString()
                    , row["Email"].ToString()
                    );
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            NhanVien obj = null;
            try
            {
                obj = new NhanVien(Convert.ToInt32(rd["ID"].ToString())
                  , rd["UserName"].ToString()
                  , rd["FullName"].ToString()
                  , rd["Birthday"].ToString() != "" ? Convert.ToDateTime(rd["Birthday"].ToString()) : (DateTime?)null
                  , rd["Password"].ToString()
                  , rd["Phone"].ToString()
                  , rd["Mobile"].ToString()
                  , rd["Department"].ToString()
                  , rd["Position"].ToString()
                  , rd["Email"].ToString()
                  );
               
            }
            catch (Exception ex){ }
            return obj;
        }
    }
}
